package com.example.qimo

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import com.example.qimo.R

class MemoAdapter(context: Context, private val memos: MutableList<String>, private val onDeleteListener: (Int) -> Unit) :
    ArrayAdapter<String>(context, 0, memos) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.item_memo, parent, false)
        val textView = view.findViewById<TextView>(R.id.textView_memo)
        textView.text = getItem(position)

        view.setOnLongClickListener {
            showDeleteConfirmationDialog(position)
            true
        }

        return view
    }

    private fun showDeleteConfirmationDialog(position: Int) {
        AlertDialog.Builder(context)
            .setTitle("Confirm Deletion")
            .setMessage("Are you sure you want to delete this memo?")
            .setPositiveButton("Delete") { _, _ ->
                onDeleteListener(position)
            }
            .setNegativeButton("Cancel", null)
            .show()
    }
}